<?php

include_once APPLICATION_DIR . "/application/model/PRODUCT_INFO.php";

class Product_Info extends DataObjects_PRODUCT_INFO
{
	public static function validateInputFromProduct(&$product)
	{
		global $error_msg;

		$product_infos = array();
		foreach($_REQUEST['PRODUCT_INFO_ID'] as $i => $id) {
			$product_info = new Product_Info();
			if ($id) {
				$product_info = $product->product_infos[$id];
				if ($_REQUEST['PRODUCT_INFO_REMOVED'][$i]) {
					$product_info->REMOVED = 1;
					$product_infos[] = $product_info;
					continue;
				}
			}

			$product_info->NAME = trim($_REQUEST['PRODUCT_INFO_NAME'][$i]);
			$product_info->DISABLED = $_REQUEST['PRODUCT_INFO_DISABLED'][$i];
			$product_info->CONTENT_NAME = $_REQUEST['PRODUCT_INFO_CONTENT_NAME'][$i];
			if ($product_info->ID) {
				$product_info->CONTENT = trim($_REQUEST["PRODUCT_INFO_CONTENT_ID_$product_info->ID"]);
			} else {
				$product_info->CONTENT = trim($_REQUEST[$product_info->CONTENT_NAME]);
			}

			if ($product_info->ID || $product_info->NAME) {
				$product_infos[] = $product_info;
			}
		}

		$product->product_infos = $product_infos;

        return $error_msg->isNoError();
	}

	public static function isExist($name, $id_product, $id = 0) {
		$product_info = new Product_Info();
		$product_info->NAME = $name;
		$product_info->ID_PRODUCT = $id_product;

		if (!empty($id)) {
			$product_info->whereAdd("ID <> $id");
		}
		$product_info->find();

		return (($product_info->N > 0) ? 1 : 0);
	}

	public static function getList($id_product = 0, $name = '') {
		$product_info = new Product_Info();
		if ($name) {
			$product_info->NAME = $name;
		}
		if ($id_product) {
			$product_info->ID_PRODUCT = $id_product;
		}

		return $product_info->fetchListObject();
	}
}
?>
